'use strict';
app.controller('agentCtrl',['$scope','utilService','$modal', function($scope, utilService, $modal){
    $scope.agent = {};
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domainAgent + 'manage/agent/list',
            params:$scope.agent
        }).then(function(result){});
    }
    $scope.query();

    //新增代理商窗口
    $scope.openAdd = function () {
        $modal.open({
            templateUrl: 'tpl/agent/agent_add.html',
            controller: 'agentAddCtrl',
            scope:$scope,
            resolve: {
                items: function () {
                    return  $scope;
                }
            }
        });
    };

    $scope.modal = {};
    // 审核通过
    $scope.auditPassed = function(id, name){
        $scope.modal.title = "审核通过";
        $scope.modal.message = "确定审核通过'"+ name +"'代理商信息？";
        utilService.modal($modal, $scope, {
            title: $scope.modal.title,
            controller: "agentAuditPassedCtrl",
            message: $scope.modal.message,
            data:{
                id: id
            }
        });
    }

    // 删除代理商信息
    $scope.openDelete = function(id, name) {
        $scope.modal.title = "删除代理信息";
        $scope.modal.message = "确定删除'"+ name +"'代理商信息？";
        utilService.modal($modal, $scope, {
            title: $scope.modal.title,
            controller: "agentDeleteCtrl",
            message: $scope.modal.message,
            data:{
                id: id
            }
        });
    }

    // 编辑
    $scope.openEdit = function (id) {
        $modal.open({
            templateUrl: 'tpl/agent/agent_edit.html',
            controller: 'agentEditCtrl',
            scope:$scope,
            resolve: {
                id: function() {
                    return id
                }
            }
        });
    };
}]);

app.controller('agentAuditPassedCtrl', ['$scope', '$modalInstance', 'agentService', 'toaster', 'modalData', '$state',
    function($scope, $modalInstance, agentService, toaster, modalData, $state) {
        $scope.isAllowSubmit=true;
        $scope.id=modalData.data.id;

        $scope.ok = function(){
            if(!$scope.isAllowSubmit){
                return;
            }
            $scope.isAllowSubmit = false;
            agentService.auditPassed($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $state.reload('app.agent');
                        break;
                    default :
                        toaster.pop('error', "提示", result.message);
                        break;
                }
                $scope.cancel();
                $scope.isAllowSubmit=true;
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

app.controller('agentDeleteCtrl', ['$scope', '$modalInstance', 'agentService', 'toaster', 'modalData',
    function($scope, $modalInstance, agentService, toaster, modalData) {
        var id = modalData.data.id;

        $scope.isAllowSubmit=true;
        $scope.ok = function(){
            if(!$scope.isAllowSubmit){
                return;
            }
            $scope.isAllowSubmit = false;
            agentService.delete($scope, id).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.query();
                        toaster.pop('success', "提示", "删除成功");
                        break;
                    default :
                        toaster.pop('error', "提示", result.message);
                        break;
                }
                $scope.cancel();
                $scope.isAllowSubmit=true;
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);